package site.teamo.biu.net.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import site.teamo.biu.net.client.core.http_https_proxy.HttpHttpsProxyService;
import site.teamo.biu.net.client.core.http_https_proxy.UpdateEvent;
import site.teamo.biu.net.client.core.http_https_proxy.constant.HttpHttpsProxyServiceMode;
import site.teamo.biu.net.client.core.http_https_proxy.state.HttpHttpsProxyState;
import site.teamo.biu.net.client.entity.ConnectionRuleDO;
import site.teamo.biu.net.client.mapper.ConnectionRuleMapper;
import site.teamo.biu.net.common.http_https_proxy.ConnectionState;
import site.teamo.biu.tool.core.model.BiuJSONResult;
import site.teamo.biu.tool.core.util.BiuJSONResultUtils;

import java.util.List;

/**
 * @@author: 爱做梦的锤子
 * @date: 2023/3/10 14:31
 */
@Tag(name = "http&https代理服务")
@RestController
@RequestMapping("/api/http-https-proxy")
public class HttpHttpsProxyController {

    @Resource
    private HttpHttpsProxyService httpHttpsProxyService;

    @Resource
    private ConnectionRuleMapper connectionRuleMapper;

    @GetMapping("/state")
    public BiuJSONResult<HttpHttpsProxyState> queryServiceState() {
        return BiuJSONResultUtils.ok(httpHttpsProxyService.state());
    }

    @Operation(summary = "更新服务")
    @PostMapping("/change-service")
    public BiuJSONResult<Boolean> changePort(@RequestParam Integer port,
                                             @RequestParam HttpHttpsProxyServiceMode mode,
                                             @RequestParam(required = false) String serverId) {
        httpHttpsProxyService.changePort(port);
        httpHttpsProxyService.changeMode(mode, serverId);
        return BiuJSONResultUtils.ok(true);
    }

    @Operation(summary = "查询连接的状态")
    @GetMapping("/query-direct-connection-state")
    public BiuJSONResult<ConnectionState> queryDirectConnectionState() {
        return BiuJSONResultUtils.ok(httpHttpsProxyService.connectionState(null));
    }

    @Operation(summary = "查询连接规则")
    @GetMapping("/query-connection-rule")
    public BiuJSONResult<List<ConnectionRuleDO>> queryConnectionRule() {
        List<ConnectionRuleDO> connectionRules = connectionRuleMapper.selectList(new QueryWrapper<>());
        return BiuJSONResultUtils.ok(connectionRules);
    }

    @Operation(summary = "删除连接规则")
    @PostMapping("/delete-connection-rule")
    public BiuJSONResult<Boolean> deleteConnectionRule(@RequestParam String id) {
        boolean flag = connectionRuleMapper.deleteById(id) > 0;
        httpHttpsProxyService.updateCall(UpdateEvent.DELETE_RULE);
        return BiuJSONResultUtils.ok(flag);
    }

    @Operation(summary = "添加或更新连接规则")
    @PostMapping("/upsert-connection-rule")
    public BiuJSONResult<Boolean> upsertConnectionRule(@RequestBody ConnectionRuleDO connectionRule) {
        boolean flag = connectionRuleMapper.upsert(connectionRule);
        httpHttpsProxyService.updateCall(UpdateEvent.UPSERT_RULE);
        return BiuJSONResultUtils.ok(flag);
    }


}
